#include <func.h>
#define N 10000000

typedef struct 
{
    int num;
    pthread_mutex_t mutex;
}add;

int main()
{
    int shmid=shmget(1000,4096,IPC_CREAT|0600);
    ERROR_CHECK(shmid,-1,"shmget");
    add *p=shmat(shmid,NULL,0);
    ERROR_CHECK(p,(add*)-1,"shmat");
    p->num=0;
    pthread_mutexattr_t mutex_attr;
    int ret=pthread_mutexattr_init(&mutex_attr);
    THREAD_ERR_CHECK(ret,"pthread_mutexattr_init");
    pthread_mutexattr_setpshared(&mutex_attr,PTHREAD_PROCESS_SHARED);
    THREAD_ERR_CHECK(ret,"pthread_mutexattr_setpshared");
    pthread_mutex_init(&p->mutex,&mutex_attr);
    THREAD_ERR_CHECK(ret,"pthread_mutex_init");
    int i;
    if(!fork())
    {
        for(i=0;i<N;i++)
        {
            pthread_mutex_lock(&p->mutex);
            p->num++;
            pthread_mutex_unlock(&p->mutex);
        }
        return 0;
    }
    else
    {
        for(i=0;i<N;i++)
        {
            pthread_mutex_lock(&p->mutex);
            p->num++;
            pthread_mutex_unlock(&p->mutex);
        }
        wait(NULL);
        printf("p->num=%d\n",p->num);
        return 0;
    }
}
